﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.22
 * 描 述：款项设置
 */
var selectedRow;
var refreshGirdData;
var userdefault = {};
var bootstrap = function ($, learun) {
    "use strict";
    var companyId = '';
    var ztId = '';
    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);
            page.bind();
            page.initGrid();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 公司选择
            $('#company_select').lrselect({
                type: 'tree',
                // placeholder: '请选择机构',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                //allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;

                        $('#zt_select').lrselectRefresh({
                            // 访问数据接口地址
                            url: config.base_server() + 'gzzts/dropdownlist?access_token=' + config.getToken().access_token,
                            // 访问数据接口参数
                            param: {companyId: companyId},
                            select: function (ztItem) {
                                if (!ztItem || ztItem.value == '-1') {
                                    ztId = "";
                                } else {
                                    ztId = ztItem.value;

                                    var keyword = $('#txt_Keyword').val();
                                    page.search({keyword: keyword});
                                }
                            }
                        }).lrselectSet(userdefault.ztId);
                    }
                }
            });
            $('#company_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);
            // 帐套选择
            $('#zt_select').lrselect({
                type: 'default',
                //placeholder: '请选择帐套',
                // 展开最大高度
                maxHeight: 300,
                height: 300,
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({keyword: keyword});
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            //初始化默认款项
            $('#lr_default').on('click', function () {
                learun.layerConfirm('初始化默认数据会覆盖现有设置，是否按照默认款项初始化该账套?', function (res) {
                    if (res) {
                        learun.postForm(config.base_server() + 'gzitems/init?companyId=' + companyId + '&ztId=' + ztId + '&access_token=' + config.getToken().access_token, {
                            companyId: companyId,
                            ztId: ztId
                        }, function () {
                            refreshGirdData();
                        });
                    }
                });
            });
            // 新增
            $('#lr_add').on('click', function () {
                if (!companyId) {
                    learun.alert.warning('请选择机构！');
                    return false;
                }
                if (!ztId) {
                    learun.alert.warning('请选择账套！');
                    return false;
                }
                selectedRow = null;
                learun.layerForm({
                    id: 'form',
                    title: '添加款项',
                    url: config.webportal_server + 'pages/salary/item/form.html?access_token=' + config.getToken().access_token + '&companyId=' + companyId + '&ztId=' + ztId,
                    width: 500,
                    height: 379,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '编辑款项',
                        url: config.webportal_server + 'pages/salary/item/form.html?access_token=' + config.getToken().access_token + '&companyId=' + companyId + '&ztId=' + ztId,
                        width: 500,
                        height: 379,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'gzitems/delete/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 设置公式
            $('#lr_formula').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '设置公式',
                        url: config.webportal_server + 'pages/salary/item/formula.html?companyId=' + companyId  + '&ztId=' + ztId + '&keyValue=' + keyValue + '&access_token=' + config.getToken().access_token,
                        width: 800,
                        height: 520,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'gzitems/list?access_token=' + config.getToken().access_token,
                headData: [
                    {label: "款项名称", name: "name", width: 300, align: "left"},
                    {label: "排序", name: "sort", width: 50, align: "center"},

                    {
                        label: "结转清零", name: "isClear", width: 60, align: "center",
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "是" : "";
                        }
                    },
                    {
                        label: "系统内置", name: "isSys", width: 60, align: "center",
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "√" : "";
                        }
                    },
                    {label: "小数位", name: "decimalPlace", width: 60, align: "center"},
                    {label: "公式", name: "formula", width: 880, align: "left"},
                    {
                        label: "更新时间", name: "updateTime", width: 140, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    {label: "备注", name: "description", width: 100, align: "left"}
                ],
                dblclick: function (row) {
                    var keyValue = $('#gridtable').jfGridValue('id');
                    selectedRow = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'form',
                            title: '编辑款项',
                            url: config.webportal_server + 'pages/salary/item/form.html?access_token=' + config.getToken().access_token + '&companyId=' + companyId,
                            width: 500,
                            height: 379,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                isPage: true,
                rows : 60,
                reloadSelected: true,
                isTree: false,
                mainId: 'id'
            });
        },
        search: function (param) {
            param = param || {};
            param.companyId = companyId;
            param.ztId = ztId;
            $('#gridtable').jfGridSet('reload', param);
        }
    };

    refreshGirdData = function () {
        page.search();
    };

    page.init();
}


